Ext.define('YXMES.smfr.controller.CreateProductSnController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'smfr_createproductsn',
		ref : 'smfrcreateproductsn'
	} ],
	requires : [ 'YXMES.smfr.store.ProductListStore', 'YXMES.smfr.store.ProdFlowList', 'YXMES.smfr.store.PathOperList', 'YXMES.smfr.store.PsnTypeList' ],
	stores : [],
	models : [],
	views : [ 'YXMES.smfr.view.CreateProductSnView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'smfr_createproductsn' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'smfr_createproductsn #btnSave' : {
				click : function() {
					return this.handle(this.onBtnSaveClick, arguments);
				}
			},
			'smfr_createproductsn #btnReset' : {
				click : function() {
					return this.handle(this.onBtnResetClick, arguments);
				}
			},
			'smfr_createproductsn #cdvPathCode' : {
				beforestoreload : function() {
					return this.handle(this.onCdvPathCodeBeforestoreload, arguments);
				}
			},
			'smfr_createproductsn #cdvOperSeq' : {
				beforestoreload : function() {
					return this.handle(this.onCdvOperSeqBeforestoreload, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'smfr_createproductsn' ? item : item.up('smfr_createproductsn'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return itemId===this.getMainView().getItemId()?this.getMainView():this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'ProductListStore')
			return this.getMainView().SbasViewProductServiceStore;
		if (name == 'ProdFlowList')
			return this.getMainView().SbasViewProductProcessPathServiceStore;
		if (name == 'PathOperList')
			return this.getMainView().SmfrViewProcessPathOperationServiceStore;
		if (name == 'PsnTypeList')
			return this.getMainView().BasViewCommonDataSetupDetailServiceStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SbasViewProductServiceStore = Ext.create('YXMES.smfr.store.ProductListStore', {
			pageSize : 30
		});
		view.SbasViewProductProcessPathServiceStore = Ext.create('YXMES.smfr.store.ProdFlowList', {
			pageSize : 30
		});
		view.SmfrViewProcessPathOperationServiceStore = Ext.create('YXMES.smfr.store.PathOperList', {
			pageSize : 30
		});
		view.BasViewCommonDataSetupDetailServiceStore = Ext.create('YXMES.smfr.store.PsnTypeList', {
			pageSize : 30
		});
		view.down('#cdvMatVer').bindStore(view.SbasViewProductServiceStore);
		view.down('#cdvPathCode').bindStore(view.SbasViewProductProcessPathServiceStore);
		view.down('#cdvOperSeq').bindStore(view.SmfrViewProcessPathOperationServiceStore);
		view.down('#cmbPsnType').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SbasViewProductServiceStore) {
			view.SbasViewProductServiceStore.destroyStore();
			view.SbasViewProductServiceStore = undefined;
		}
		if (view.SbasViewProductProcessPathServiceStore) {
			view.SbasViewProductProcessPathServiceStore.destroyStore();
			view.SbasViewProductProcessPathServiceStore = undefined;
		}
		if (view.SmfrViewProcessPathOperationServiceStore) {
			view.SmfrViewProcessPathOperationServiceStore.destroyStore();
			view.SmfrViewProcessPathOperationServiceStore = undefined;
		}
		if (view.BasViewCommonDataSetupDetailServiceStore) {
			view.BasViewCommonDataSetupDetailServiceStore.destroyStore();
			view.BasViewCommonDataSetupDetailServiceStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onBtnSaveClick : function(button, e, eOpts) {
		if (!this.getItem('frmMain').getForm().isValid()) {
		    return;
		}
		
		var parms = this.getItem('frmMain').getValues();
		
		var me = this;
		button.disable();
		YX.ZbusRpc.request({
		    module: 'SmfrCreateProductSerialService',
		    params: parms,
		    showFail: false,
		    showSuccess: false,
		    callback: function() {
		        button.enable();
		        me.getItem('txtPsn').reset();
		        me.getItem('dspPsn').setValue(parms.psn);
		    },
		    success: function(rsp) {
		        me.getItem('dspResult').setValue(rsp.rspDesc);
		        me.getItem('txtPsn').addCls(['bold', 'black']);
		        me.getItem('txtPsn').removeCls(['bold', 'red']);
		    },
		    failure: function(rsp) {
		        var msg = rsp.rspDesc;
		        if (msg.length > 50) {
		            msg = msg.substr(0, 100) + "..";
		        }
		        me.getItem('dspResult').setValue(msg);
		        me.getItem('txtPsn').removeCls(['bold', 'black']);
		        me.getItem('txtPsn').addCls(['bold', 'red']);
		    }
		});
	},
	onBtnResetClick : function(button, e, eOpts) {
		this.getItem('frmMain').getForm().reset();
	},
	onCdvPathCodeBeforestoreload : function(btn, store, params, selected) {
		Ext.apply(params, {
		    matCode: this.getItem('txtMatCode').getValue(),
		    matVer: this.getItem('cdvMatVer').getValue()
		});
	},
	onCdvOperSeqBeforestoreload : function(btn, store, params, selected) {
		params.pathCode = this.getItem('cdvPathCode').getValue();
	},
	loadProductListStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ProductListStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadProdFlowList : function(params, options) {
		params = Ext.apply(params || {},
		{
		    // Default params
		    matCode: this.getItem('txtMatCode').getValue(),
		    matVer: this.getItem('cdvMatVer').getValue()
		});
		
		var store = this.getStore('ProdFlowList');
		if (!store) {
		    return false;
		}
		
		store.getProxy().extraParams = params;
		if (store.loadPage) {
		    store.loadPage(1, options);
		} else {
		    store.load(options);
		}
	},
	loadPathOperList : function(params, options) {
		params = Ext.applyIf(params || {},
		{
		    // Default params
		    pathCode: this.getItem('cdvPathCode').getValue()
		});
		
		var store = this.getStore('PathOperList');
		if (!store) {
		    return false;
		}
		
		store.getProxy().extraParams = params;
		if (store.loadPage) {
		    store.loadPage(1, options);
		} else {
		    store.load(options);
		}
	},
	loadPsnTypeList : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('PsnTypeList');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	}
});